#! /bin/bash
#PBS -N DistSVD
#PBS -l walltime=02:00:00
#PBS -q class
#PBS -o output/DistSVD.out
#PBS -e output/DistSVD.err
#PBS -d /nethome/jchua3/vetter/cse6220/project/
#PBS -v np=4
#PBS -l nodes=4:m2090

cd $PBS_O_WORKDIR
echo "*****we are in "
echo $PBS_O_WORKDIR
echo "*****"

#run R on all the nodes we want
`which mpirun` -n 1 -machinefile $PBS_NODEFILE R --vanilla > output/DistSVD.R.out <<EOF

# SCRIPT TO MODIFY FOR THE DIFFERENT EXPERIMENTS WE WANT
# FOR THE APPROXIMATE SVD GPU KERNEL

library(gputools)
library(MASS) 

#set working directory of main source path file and source it
setwd("/nethome/jchua3/vetter/cse6220/project/")
source("source.R")

#start up the MPI cluster with np instances of R, running on all
#available nodes
cl <- startCluster($np)

#prepare data
size <- 1024
projdim <- 32
ens_size <- $np
results <- list()

 
    #construct data
    A = mat.or.vec(size, size)
    A = A+0.5
    U = mat.or.vec(size, 32)
    U = U+1.0
    V = mat.or.vec(size, 32)
    V = V+1.0
    data = list(A) #data must be kept in a list
#    data = list(A,A,A)

    #run svd using ensemble task pull and save results
    #in this case we're not doing random sampling
    results <- taskPullEnsemble(ens_size,size,cl,data,gpuSvd,U,V,projdim,100, -0.000001)

#    results <- distTime(taskPullNormal(cl,data,gpuSvd,U,V,projdim,100,-0.000001))
#    results <- taskPullNormal(cl,data,gpuSvd,U,V,projdim,100,-0.000001)
#    results <- distTime(taskPullNormal(cl,data,gpuSvd,U,V,projdim,50,-0.000001))
#    results <- taskPullNormal(cl,data,gpuSvd,U,V,projdim,50,-0.000001)

#compute reconstruction error
#only use if we're computing the approximation, not for timing
U_list <- list()
V_list <- list()
for (i in 1:length(results)) {
    U_list[[i]] <- results[[i]][[1]]
    V_list[[i]] <- results[[i]][[2]]
}
U_global <- Reduce("+",U_list) / length(U_list)
V_global <- Reduce("+",V_list) / length(V_list)
norm_approx <- norm(U_global %*% V_global)
norm_actual <- norm(A)

#save results
saveList = c("norm_approx", "norm_actual", "results")
# saveList = c("results")
save(list = saveList, file="output/DistSVD.Rdata")
stopCluster()
mpi.quit()


EOF



